//
//  main.m
//  05_遍历Code
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 yh. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
        // 将数组倒序输出 存放在一个可变数组中
        // 首先创建一个不可变数组 让后用可变数组接收
        NSArray *arr = @[@"伟仔", @"阿美", @"飞哥", @"糖糖", @"瑞瑞"];
        NSMutableArray *mutArr = [NSMutableArray array];
        for (NSInteger i = arr.count - 1; i >= 0; i--) {
            [mutArr addObject:arr[i]];
        }
        NSLog(@"%@", mutArr);
    
        // 定义一个字典 遍历输出 name sex age
        NSDictionary *dic = @{@"name": @"陈冠希", @"sex": @"man", @"age": @"35"};
        NSArray *keyArr = dic.allKeys;
        for (NSInteger i = 0; i < keyArr.count; i++) {
            NSString *key = keyArr[i];
            NSString *value = [dic objectForKey:key];
            NSLog(@"%@ = %@", key, value);
    }

    // 正序 枚举器 依赖于数组 字典 集合 不能单独使用
    NSEnumerator *stringArrayEnumerator = [arr objectEnumerator];
    id value = nil;
    while ((value = [stringArrayEnumerator nextObject])) {
    NSLog(@"%@", value);
    }
    // 倒序输出
    NSEnumerator *rever = [arr reverseObjectEnumerator];
    id value1 = nil;
    while ((value1 = [rever nextObject])) {
        NSLog(@"%@", value1);
    }
    
    // 字典 枚举器 默认打印字典的value 也可以打印所有的key值 无序的
    NSEnumerator *dicEnumerator = [dic.allKeys objectEnumerator];
    id value2 = nil;
    while (value2 = [dicEnumerator nextObject]) {
//        NSArray *allKey = [dic allKeysForObject:value2]; 取key
        NSLog(@"%@", value2);
    }
    
    // 集合枚举器 无序的
    NSSet *set = [NSSet setWithObjects:@"q", @"w", @"e", @"r", nil];
    NSEnumerator *setEnu = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnu nextObject]) {
        NSLog(@"%@", setValue);
    }
    
    // 快速枚举 遍历 for...in...  封装了 枚举
    NSString *newStr = [NSString string];
    for (NSString *string in arr) {
        NSLog(@"%@", string);
        newStr = [newStr stringByAppendingString:string];
    }
    NSLog(@"%@", newStr);
    
    // 字典快速遍历
    for (NSString *dicStr in dic) {
        NSLog(@"%@", dicStr);
        NSLog(@"%@", [dic objectForKey:dicStr]);
    }
    // 集合 快速遍历
    for (NSString *setStr in set) {
        NSLog(@"%@", setStr);
    }
    
    // 排序
    // 参数1 key: 排序条件 参数2 acending YES(升序) / NO(降序)
    // 数组有排序的方法
    NSArray *numArr = @[@1, @7, @5, @4];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    numArr = [numArr sortedArrayUsingDescriptors:@[sort]];
    NSLog(@"num: %@", numArr);
    
    // 可变数组与不可变数组 排序使用的方法不同
    NSMutableArray *numMutArr = [NSMutableArray arrayWithObjects:@"1", @"5", @"6", @"2", nil];
    NSSortDescriptor *mutSort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    [numMutArr sortUsingDescriptors:@[mutSort]];
    NSLog(@"%@", numMutArr);
    
    Person *per1 = [[Person alloc] init];
    per1.name = @"冠希";
    per1.age = 35;
    
    Person *per2 = [[Person alloc] init];
    per2.name = @"伟仔";
    per2.age = 48;
    
    Person *per3 = [[Person alloc] init];
    per3.name = @"jj";
    per3.age = 34;
    
    NSArray *perArr = @[per1, per2, per3];
    NSLog(@"%@", perArr);
    // 根据name  age进行排序
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];
    NSArray *ageArr = [perArr sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"%@", ageArr);
    
    // 排序 字符串 默认升序排列
    NSArray *strArray = @[@"zhaohao", @"baobao", @"tantan", @"huaidan"];
    NSArray *newStrArr = [strArray sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", newStrArr);
    
    // for ... in 倒序
    NSArray *numberArr = @[@7, @17, @27, @37];
    for (NSNumber *num in [numberArr reverseObjectEnumerator]) {
        NSLog(@"%@", num);
    }
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}
